<?php
class RoleAction extends BaseAction {
	function index(){
		$idx = isset($_GET['idx'])?$_GET['idx']:0;
		$role=new RoleModel();
		import('ORG.Util.Page');
		$count=$role->count();
		$page=new Page($count,C('PAGESIZE'));
		$show=$page->show();
		$this->assign("page",$show);
		//需要查询哪些字段
        $fields = 'id,name,note';
		$list=$role->order('id')->field($fields)->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('list',$list);
		//功能模块按钮
		$option = get_option($idx);
		$this->assign('coption',$option);
		$this->display();
	}

	function insert(){
		$tmpPOST = $_POST;
		unset($_POST['role'],$_POST['resource']);
		$role=new RoleModel();
		if($data=$role->create()){
			$role->startTrans();
			$result = $role->add();
			if($result){
				$autoid=$role->getLastInsID();
				if($autoid>0 && $result){
					$iRes = $tmpPOST['resource'];
					$rPermission = new RolePermissionModel();
					$sql="insert into __TABLE__ (`rid`,`fid`,`c`,`u`,`d`) values";
					$sqlbyte = 65;
					if($iRes==1){//选择
						$arrRoles =	$tmpPOST['role'];
						foreach($arrRoles as $fid=>$arr){
							$c = $u = $d =0;
							if(empty($arr)){
								$c = $u = $d = 1;
							}else{
								foreach($arr as $k=>$v){
									switch ($v) {
										case CRUD_C:$c=1;break;
										case CRUD_U:$u=1;break;
										case CRUD_D:$d=1;break;
									}
								}
							}
							$sql.=cstrlen($sql)>$sqlbyte?",($autoid,$fid,$c,$u,$d)":"($autoid,$fid,$c,$u,$d)";
						}
					}else{//全选
						$fun=new FunctionModel();
						$rlist=$fun->field('id')->select();
						foreach($rlist as $arr){
							$c = $u = $d = 1;$fid=$arr['id'];
							$sql.=cstrlen($sql)>$sqlbyte?",($autoid,$fid,$c,$u,$d)":"($autoid,$fid,$c,$u,$d)";
						}
					}
					if(cstrlen($sql)>$sqlbyte){
						$num = $rPermission->execute($sql);
						if($num<1){
							$result = false;
						}
					}
				}
			}

			if($result){
				/*---------------操作日志-----START----------*/
				import("@.Util.Slog");
				Slog::getInstance()->write(CRUD_C,$tmpPOST);
				/*---------------操作日志-----END------------*/
				$role->commit();
				$this->assign('jumpUrl',__URL__.'/index');
				$this->success('操作成功，插入数据编号为：'.$autoid);
			}else{
				$role->rollback();
				$this->error('操作失败：系统异常');
			}
		}else{
			$this->error('操作失败：数据验证( '.$role->getError().' )');
		}
	}

	function update(){
		$tmpPOST = $_POST;
		unset($_POST['role']);
		$role=new RoleModel();
		if($data=$role->create()){
			$rid = 0;
			if(!empty($data['id'])){
				$rid = $data['id'];
				$role->save();
			}else{
				$this->error('请选择编辑用户');die();
			}

			$arrRoles =	$tmpPOST['role'];
			if(!empty($arrRoles) && $rid>0){
				$rPermission = new RolePermissionModel();
				$rPermission->startTrans();
				$sql="insert into __TABLE__ (`rid`,`fid`,`c`,`u`,`d`) values";
				$sqlbyte = 65;
				foreach($arrRoles as $fid=>$arr){
					$c = $u = $d =0;
					if(empty($arr)){
						$c = $u = $d = 1;
					}else{
						foreach($arr as $k=>$v){
							switch ($v) {
								case CRUD_C:$c=1;break;
								case CRUD_U:$u=1;break;
								case CRUD_D:$d=1;break;
							}
						}
					}
					$sql.=cstrlen($sql)>$sqlbyte?",($rid,$fid,$c,$u,$d)":"($rid,$fid,$c,$u,$d)";
				}
				$result = false;
				if(cstrlen($sql)>$sqlbyte){
					$delSql ="DELETE FROM __TABLE__ WHERE (`rid` = $rid);";
					$num = $rPermission->execute($delSql);
					$num += $rPermission->execute($sql);
					$result = $num<1?false:true;
				}

				if($result){
					/*---------------操作日志-----START----------*/
					import("@.Util.Slog");
					Slog::getInstance()->write(CRUD_U,$tmpPOST);
					/*---------------操作日志-----END------------*/
					$rPermission->commit();
					$this->assign('jumpUrl',__URL__.'/index');
					$this->success("操作成功");
				}else{
					$rPermission->rollback();
					$this->error('操作失败：系统异常');
				}
		}else{
			$this->error('操作失败：数据验证( '.$user->getError().' )');
		}
		}
	}

	function add(){
		$fun=new FunctionModel();
		$flist=$fun->order('parent_id')->field('id,parent_id,caption,node_url')->select();
		$this->assign('rlist',$flist);
		$this->display();
	}

	function edit(){
		$id=$_GET['id'];
		if(!empty($id)){
			//角色模型
			$role=new RoleModel();
			$date=$role->field('id,name,note')->getById($id);
			$this->assign('obj',$date);
			//权限
			$fun=new FunctionModel();
			$rlist=$fun->order('parent_id')->field('id,parent_id,caption,node_url')->select();
			//角色权限
			$rolePermission =new RolePermissionModel();
			$rOwn = $rolePermission->field('fid,c,u,d')->where(array('rid'=>$id))->findAll();
			$rlown= $newList = array();
			foreach($rOwn as $arrModel){
				$rlown[$arrModel['fid']]=array('s'=>$arrModel['c']+$arrModel['u']+$arrModel['d'],'c'=>$arrModel['c'],'u'=>$arrModel['u'],'d'=>$arrModel['d']);
			}
			foreach($rlist as $arr){
				if(isset($rlown[$arr['id']])){
					$arr['own']=$rlown[$arr['id']];
				}
				$newList[]=$arr;
			}
			$this->assign('rlist',$newList);
			//按钮权限
			$option = $_SESSION['soption'];
			$this->assign('coption',$option);
		}
		$this->display();
	}

	function delete(){
		$did=$_POST['did'];
		if(!empty($did) && is_array($did)){
			$role=new RoleModel();
			$id=implode(',',$did);
			if(false!==$role->delete($id)){
				//删除对应的角色权限
				$rPermission = new RolePermissionModel();
				$delSql ="DELETE FROM __TABLE__ WHERE (`rid` = $id);";
				$num = $rPermission->execute($delSql);
				if($num>0){
					/*---------------操作日志-----START----------*/
					import("@.Util.Slog");
					Slog::getInstance()->write(CRUD_U,$did);
					/*---------------操作日志-----END------------*/
					$this->assign('jumpUrl',__URL__.'/index');
					$this->success('操作成功');
				}else{
					$this->error('操作失败：'.$rPermission->getDbError());
				}
			}else{
				$this->error('操作失败：'.$role->getDbError());
			}
		}else{
			$this->error('请选择删除用户');
		}
	}
}
?>